var ns, stoptime, leftElem, popup;
var Vheight = window.clientHeight;
app = new Vue({
    el: '#app',
    data: {
        form: {
            status: 1,
            ischeckpage: false,
            pageindex: 0,
            pagesize: 10,
            PushType: 0,
        },
        countform: {
            unreadcount: 0
        },
        menuList: [],
        list: [],
        current_item_length: 0,
        scroll_top: 0,
        can_scroll: false,
        showbg: false,
        isopen: false,
        showsearch: false,
        actived: '',
        hasdatas: false,

    },
    methods: {
        get_data: function () {
            var that = this;
            var options = {};
            // that.list = [];
            options.action = "getpushmsglist";
            options.pagesize = that.form.pagesize;
            options.pageindex = that.form.pageindex;
            options.PushType = that.form.PushType;
            ns.post(options, function (succeed, data, err) {
                if (succeed) {

                    if (data.list.length == that.form.pagesize) {
                        that.can_scroll = true;
                    }
                    if (that.form.pageindex == 0) {
                        that.menuList = data.headList;
                        api.sendEvent({
                            name: 'reloadunreadmsgcount'
                        });
                        that.list = data.list;
                    } else {
                        that.list = that.list.concat(data.list);
                    }

                    app.current_item_length = that.list.length;
                    // app.countform.unreadcount = data.unreadcount;
                } else {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                toast: true
            });
        },

        // select_status: function(status) {
        //     var that = this;
        //     that.form.status = status;
        //     that.form.pageindex = 0;
        //     that.get_data();
        // },
        openGongdan: function (item) {
            ns.openWin('home_task_con_frm', '任务', {
                id: item.RelatedID
            });
        },
        do_open_newslist: function (index, status) {
            var that = this;
            that.form.pageindex = 0;
            that.actived = index;
            that.form.PushType = status;
            setTimeout(function () {
                that.get_data();
            }, 500);
        },
        openWin: function (item, params) {
            var that = this;
            if (item.url == '') {
                return;
            }
            params = params || {
                status: -1
            };
            ns.openWin(item.url, item.name, params);
        },
        open_mail: function () {
            ns.openWin('home_news_mail_frm', '通讯录', {
                mail: 5
            });
        },
        //全部删除
        delete_all: function () {
            var that = this;
            var options = {};
            options.action = "doremoveallpushmsg";
            ns.post(options, function (succeed, data, err) {
                if (succeed) {
                    api.sendEvent({
                        name: 'reloadunreadmsgcount'
                    });
                    that.isopen = false,
                        that.showsearch = false,
                        setTimeout(function () {
                            that.get_data();
                        }, 500);

                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                toast: true
            })
        },
        read_all: function (ID) {
            var that = this;
            var options = {};
            options.action = "readpushmsg";
            options.ID = 0;
            ns.post(options, function (succeed, data, err) {
                api.sendEvent({
                    name: 'reloadunreadmsgcount'
                });
                if (succeed) {
                    that.isopen = false,
                        that.showsearch = false,
                        setTimeout(function () {
                            that.form.pageindex = 0;
                            that.get_data();
                        }, 500);
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                toast: true
            });
        },
        get_read_state: function (index) {
            var that = this;
            if (that.form.status == 2) {
                return;
            }
            var options = {};
            options.action = "gettotalundreadpushmsgcount";
            options.onlymsg = 1;
            ns.post(options, function (succeed, data, err) {
                if (succeed) {
                    that.countform.unreadcount = data.count;
                }
            });
            // that.list.splice(index, 1);
        },
        read_pushmsg: function (item, index, canopen) {
            var that = this;


            item.showbtn = false;
            // that.showbg = item.showbtn;
            var options = {};
            options.action = "readpushmsg";
            options.ID = item.ID;
            options.MsgType = item.MsgType;
            ns.post(options, function (succeed, data, err) {
                api.sendEvent({
                    name: 'reloadunreadmsgcount'
                });
                if (succeed) {
                    that.get_read_state(index);
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
                // if (!canopen) {
                //     return;
                // }902报事报修，901投诉建议，903投诉建议

                if (item.LogCategory == 1 || item.LogCategory == 2) {
                    ns.openWin('home_task_con_frm', '工单详情', {
                        id: item.RelatedID,
                    });

                    setTimeout(function () {
                        that.get_data();
                    }, 500);
                    return;
                }

                if (item.LogCategory == 8) {
                    ns.openWin('qualitycontroldetail_frm', '工单详情', {
                        id: item.RelatedID
                    });
                    setTimeout(function () {
                        that.get_data();
                    }, 500);
                    return;
                }
                if (item.LogCategory == 9) {
                    ns.openWin('tongzhixiaoxidetail_frm', '通知公告', {
                        id: item.RelatedID
                    });
                    setTimeout(function () {
                        that.get_data();
                    }, 500);
                    return;
                }
                if(item.LogCategory == 20){
                    ns.openWin('examine_detail', '审批详情', {
                        ApprovalID: item.RelatedID
                    });
                    setTimeout(function () {
                        that.get_data();
                    }, 500);
                    return;
                }
                if (item.LogCategory == 3) {
                    var url = 'equipment_list_frm';
                    var title = '任务开始';
                    if (data.TaskStatus == 1) {
                        url = 'equipment_summary_frm';
                        title = '任务详情';
                    }
                    ns.openWin(url, title, {
                        id: item.RelatedID
                    });
                    setTimeout(function () {
                        that.get_data();
                    }, 500);
                    return;
                }
                if (item.LogCategory == 4) {
                    // var url = 'patrolplan_list_frm';
                    var url='patrolplan_list_frm_new';
                    var title = '任务开始';
                    if (data.TaskStatus == 1) {
                        url = 'patrolplan_summary_frm';
                        title = '任务详情';
                    }
                    ns.openWin(url, title, {
                        id: item.RelatedID
                    });
                    setTimeout(function () {
                        that.get_data();
                    }, 500);
                    return;
                }
                if (item.LogCategory == 5) {
                    var url = 'bjlh_list_frm';
                    var title = '任务开始';
                    if (data.TaskStatus == 1) {
                        url = 'bjlh_summary_frm';
                        title = '任务详情';
                    }
                    ns.openWin(url, title, {
                        id: item.RelatedID
                    });
                    setTimeout(function () {
                        that.get_data();
                    }, 500);
                    return;
                }
                api.toast({
                    msg: '暂未开放该类型',
                    duration: 2000,
                    location: 'bottom'
                });
            })
        },
        //查询工单是否存在
        select_customer: function (item, index, canopen) {
            var that = this;
            var options = {};
            if (item.LogCategory == 1) {
                options.ID = item.RelatedID;
                options.action = "getnewservicedetail";//报事报修工单详情
            }
            if (item.LogCategory == 8) {
                options.ID = item.RelatedID;
                options.action = "getqualitycontroldetail";//工单详情
            }
            if (item.LogCategory == 9) {
                options.ID = item.RelatedID;
                options.action = "getmsgdetail"; //通知公告详情
            }
            if (item.LogCategory == 20) {
                options.ApprovalID = item.RelatedID;
                options.action = "getapprovalcenterdetail"; //审批详情
            }
            if (item.LogCategory == 3) {
                if (data.TaskStatus == 1) {
                    options.ID = item.RelatedID;
                    options.action = "getequipmenttaskdata"; //设备详情
                }
                else {
                    options.ID = item.RelatedID;
                    options.action = "getequipmenttaskdevicelist"; //设备列表
                }
            }
            if (item.LogCategory == 5) {
                if (data.TaskStatus == 1) {
                    options.ID = item.RelatedID;
                    options.action = "getequipmenttaskdata"; //绿化保洁详情
                }
                else {
                    options.ID = item.RelatedID;
                    options.action = "getequipmenttaskdevicelist"; //设备列表
                }
            }
            ns.post(options, function (succeed, data, err) {
                if (succeed) {
                    that.read_pushmsg(item, index, canopen);
                } else if (err) {
                    api.toast({
                        msg: '该工单不存在',
                        duration: 2000,
                        location: 'bottom'
                    });
                    that.do_remove(item, index);
                    return;
                }
            });
        },
        is_checkpage_changeStyle: function () {
            var that = this;
            if (that.form.ischeckpage == true) {
                var el = $api.byId('smart');
                $api.css(el, 'margin-top:0;');
            }
        },
        openWin: function (item, params) {
            var that = this;
            if (item.url == '') {
                return;
            }
            params = params || {
                status: -1
            };
            ns.openWin(item.url, item.name, params);
        },
        do_showbtn: function (item) {
            var that = this;
            for (var i = 0; i < that.list.length; i++) {
                that.list[i].showbtn = false;
            }
            item.showbtn = !item.showbtn;
            that.showbg = item.showbtn;
        },
        do_remove: function (item, index) {
            var that = this;
            var options = {};
            options.action = "doremovepushmsg";
            options.ID = item.ID;
            ns.post(options, function (succeed, data, err) {
                api.sendEvent({
                    name: 'reloadunreadmsgcount'
                });
                that.list.splice(index, 1);
            })


            item.showbtn = false;
            that.showbg = item.showbtn;
        },
        hide_bg: function () {
            var that = this;
            that.showbg = false;
            for (var i = 0; i < that.list.length; i++) {
                that.list[i].showbtn = false;
            }
        },
        showpopup: function () {
            var that = this;
            that.isopen = !that.isopen;
            that.showsearch = !that.showsearch;
        },
        doHideSearch: function () {
            var that = this;
            that.isopen = false;
            that.showsearch = false;
        },

    }
});
apiready = function () {
    api.parseTapmode();
    ns = window.Foresight.Util;
    app.form.ischeckpage = api.pageParam.ischeckpage || false;
    ns.init_pull();
    app.is_checkpage_changeStyle();
    app.get_data();
    api.addEventListener({
        name: 'ajpushnotupdate'
    }, function (ret, err) {
        if (ret) {
            app.get_data();
        } else { }
    });
    api.addEventListener({
        name: 'home_new_updata'
    }, function (ret, err) {
        if (ret) {
            app.get_data();
        } else { }
    });
    api.addEventListener({
        name: 'do_read_all_msg'
    }, function (ret, err) {
        app.read_all();
    });
    api.addEventListener({
        name: 'openpopup'
    }, function (ret, err) {
        app.showpopup();
    });
};
